package top.lixunda.ecommerce.server.good.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseServiceImpl;
import top.lixunda.ecommerce.server.good.api.entity.db.StoreDO;
import top.lixunda.ecommerce.server.good.api.entity.db.StoreMasterDO;
import top.lixunda.ecommerce.server.good.mapper.StoreMapper;
import top.lixunda.ecommerce.server.good.mapper.StoreMasterMapper;
import top.lixunda.ecommerce.server.good.service.IStoreMasterService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Xumda
 * @since 2019-12-18
 */
@Service
public class StoreMasterServiceImpl extends BaseServiceImpl<StoreMasterMapper, StoreMasterDO>
        implements IStoreMasterService {

    private final StoreMapper storeMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public StoreMasterServiceImpl(StoreMapper storeMapper) {
        this.storeMapper = storeMapper;
    }

    /**
     * 根据店主信息id查询店主信息，管理员调用
     *
     * @param id 店主信息id
     * @return 店主信息
     */
    @SuppressWarnings("InfiniteRecursion")
    @Override
    public StoreMasterDO queryMasterById(Integer id) {
        if (ObjectValidators.isEmpty(id)) {
            throw getAppException("信息id不能为空");
        }
        StoreMasterDO storeMasterDO = this.queryMasterById(id);
        if (ObjectValidators.isEmpty(storeMasterDO)) {
            throw getAppException("信息不存在");
        }
        return storeMasterDO;
    }

    /**
     * 根据店主信息ids查询店主信息，管理员调用
     *
     * @param ids 店主信息id列表
     * @return 店主信息列表
     */
    @Override
    public List<StoreMasterDO> queryMasterByIds(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            throw getAppException("参数不能为空");
        }
        return (List<StoreMasterDO>) this.listByIds(ids);
    }

    /**
     * 根据店铺id查询店铺店主信息
     *
     * @param id 店铺id
     * @return 店主信息列表
     */
    @Override
    public StoreMasterDO queryMasterByStoreId(Integer id) {
        if (ObjectValidators.isEmpty(id)) {
            throw getAppException("店铺id不能为空");
        }
        StoreDO storeDO = storeMapper.selectById(id);
        if (ObjectValidators.isEmpty(storeDO)) {
            throw getAppException("店铺不存在");
        }
        if (ObjectValidators.isEmpty(storeDO.getStoreMasterId())) {
            throw getAppException("店家信息不存在");
        }
        StoreMasterDO storeMasterDO = this.getById(storeDO.getStoreMasterId());
        if (ObjectValidators.isEmpty(storeMasterDO)) {
            throw getAppException("店家信息不存在");
        }
        return storeMasterDO;
    }

    /**
     * 根据店铺ids查询店铺店主信息列表
     *
     * @param ids 店铺ids
     * @return 店主信息列表
     */
    @Override
    public List<StoreMasterDO> queryMasterByStoreIds(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            throw getAppException("店铺id列表不能为空");
        }
        List<StoreDO> list = storeMapper.selectBatchIds(ids);
        if (ObjectValidators.isEmpty(list)) {
            throw getAppException("店铺不存在");
        }
        List<Integer> masterIds = list.stream()
                .map(StoreDO::getStoreMasterId)
                .collect(Collectors.toList());
        List<StoreMasterDO> resultList = (List<StoreMasterDO>) this.listByIds(masterIds);
        if (ObjectValidators.isEmpty(resultList)) {
            throw getAppException("店家信息不存在");
        }
        return resultList;
    }

}
